{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 功能一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "from datetime import datetime\n",
    "import shlex\n",
    "import logging\n",
    "import argparse\n",
    "import string\n",
    "from decimal import Decimal\n",
    "from collections import defaultdict\n",
    "import pandas as pd\n",
    "import numpy as npa\n",
    "from datetime import timedelta\n",
    "import tqdm\n",
    "import sys\n",
    "import gc\n",
    "from memory_profiler import profile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "del all_dict[\"600166\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "peak memory: 108.55 MiB, increment: 0.00 MiB\n"
     ]
    }
   ],
   "source": [
    "%memit all_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数\n",
    "def dateToYYYYMMDD(rawDate):\n",
    "    # date_ISO = datetime.strptime(rawDate, '%d/%m/%y').date()\n",
    "    date_ISO = datetime.strptime(rawDate, '%Y%m%d').date()\n",
    "    date_Str = date_ISO.strftime('%Y%m%d')\n",
    "    return date_Str\n",
    "\n",
    "\n",
    "def timetoHHMMSSF(rawTime):\n",
    "    time_ISO = datetime.strptime(rawTime, '%H:%M:%S.%f')\n",
    "    return time_ISO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test10(all_dict,time_delta,fluc_range,fluc_money,in_volume,time,price,SID):\n",
    "    time10=time-timedelta(minutes=time_delta)\n",
    "    fluc_range=float(fluc_range)\n",
    "    fluc_money=float(fluc_money)\n",
    "    M_B=0\n",
    "    M_S=0\n",
    "    a=0\n",
    "    p_max=price\n",
    "    p_min=price\n",
    "    for t in list(all_dict[SID].keys()):\n",
    "        if t>=time10:\n",
    "            for I in list(all_dict[SID][t].keys()):\n",
    "                flag=all_dict[SID][t][I][2]\n",
    "                if flag==\"B\":\n",
    "                    v=all_dict[SID][t][I][0]\n",
    "                    M_B+=all_dict[SID][t][I][1]*v\n",
    "                    p_now=all_dict[SID][t][I][1]\n",
    "                    if (p_now>p_max):\n",
    "                        p_max=p_now\n",
    "                    elif (p_now<p_min):\n",
    "                        p_min=p_now\n",
    "                    else:\n",
    "                        pass\n",
    "                elif flag==\"S\":\n",
    "                    v=all_dict[SID][t][I][0]\n",
    "                    M_S+=all_dict[SID][t][I][1]*v\n",
    "                    p_now=all_dict[SID][t][I][1]\n",
    "                    if (p_now>p_max):\n",
    "                        p_max=p_now\n",
    "                    elif (p_now<p_min):\n",
    "                        p_min=p_now\n",
    "                    else:\n",
    "                        pass\n",
    "                else:\n",
    "                    pass\n",
    "        else:\n",
    "            del(all_dict[SID][t])\n",
    "    p=round((p_max/p_min-1),6)\n",
    "    M=round(M_B-M_S,2)\n",
    "    if (p>=fluc_range)&(p<=0.1)&(M>=fluc_money):\n",
    "        a=1\n",
    "    else:\n",
    "        pass\n",
    "    return all_dict,a,p,M\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<string>:9: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
      "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "723feb46949e40b88de851cb7171d7ee",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "价格波动幅度：0.07013,资金净流入：118445884.96,股票代码：600166,时间： 09:36:39.910000,交易人员代码:1070760\n",
      "价格波动幅度：0.092437,资金净流入：32598254.96,股票代码：601236,时间： 10:14:07.940000,交易人员代码:3419549\n",
      "\n",
      "peak memory: 119.77 MiB, increment: 0.00 MiB\n"
     ]
    }
   ],
   "source": [
    "% % memit\n",
    "dataline = csv.DictReader(open(\"simple_data.csv\"))\n",
    "volume_dict = defaultdict(lambda: defaultdict(\n",
    "    lambda: defaultdict(float)))  # 股票-时间-人-订单量\n",
    "all_dict = defaultdict(lambda: defaultdict(\n",
    "    lambda: defaultdict(lambda: None)))  # 股票-时间-人-（总订单量，价格,flag)\n",
    "last_SID = defaultdict(lambda: None)  # 只显示一条\n",
    "last_price = defaultdict(\n",
    "    lambda: [0.001, timetoHHMMSSF(\"9:20:00.00\")])  # 上一次达到条件时的股票对应的价格,时间\n",
    "\n",
    "time_delta = 10\n",
    "fluc_range = 0.07\n",
    "fluc_money = 30000000\n",
    "in_volume = 200000\n",
    "\n",
    "for order in tqdm.tqdm_notebook(dataline):\n",
    "    SID = order[\"SecurityID\"]\n",
    "    time = timetoHHMMSSF(order[\"TradTime\"])\n",
    "    price = float(order[\"TradPrice\"])\n",
    "    volume = float(order[\"TradVolume\"])\n",
    "    flag = order[\"TradeBSFlag\"]\n",
    "    SellID = order[\"TradeSellNo\"]\n",
    "    BuyID = order[\"TradeBuyNo\"]\n",
    "    money = float(order[\"TradeMoney\"])\n",
    "    list_SID = ['204001', '204002', '204003', '204004', '204005', '204006',\n",
    "                '204007', '204014', '204028', '204091', '204182', '019654', '019658']\n",
    "    ID = None\n",
    "    if flag == \"B\":\n",
    "        ID = BuyID\n",
    "    elif flag == \"S\":\n",
    "        ID = SellID\n",
    "    else:\n",
    "        continue\n",
    "    if SID not in list_SID:\n",
    "        volume_dict[SID][time][ID] += volume\n",
    "        all_dict[SID][time][ID] = (volume_dict[SID][time][ID], price, flag)\n",
    "    else:\n",
    "        continue\n",
    "    # 先将十分钟以外的数据删除\n",
    "    t_max = list(all_dict[SID].keys())[-1]\n",
    "    for t in list(all_dict[SID].keys()):\n",
    "        if (t_max-t) >= timedelta(minutes=time_delta):\n",
    "            del(all_dict[SID][t])\n",
    "        else:\n",
    "            break\n",
    "#         gc.collect()\n",
    "    if volume_dict[SID][time][ID] >= in_volume and last_SID[SID] != (time, ID) and (price/last_price[SID][0]-1) >= 0.07 and \\\n",
    "            (time-last_price[SID][1] >= timedelta(minutes=time_delta)):\n",
    "        all_dict, a, p, M = test10(\n",
    "            all_dict, time_delta, fluc_range, fluc_money, in_volume, time, price, SID)\n",
    "        if a == 1:\n",
    "            print(\"价格波动幅度：{},资金净流入：{},股票代码：{},时间：{},交易人员代码:{}\".format(\n",
    "                p, M, SID, str(time)[10:], ID))\n",
    "            last_SID[SID] = (time, ID)\n",
    "            last_price[SID] = [price, time]\n",
    "    else:\n",
    "        continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext memory_profiler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": {
       "cell": {
        "!": "OSMagics",
        "HTML": "Other",
        "SVG": "Other",
        "bash": "Other",
        "capture": "ExecutionMagics",
        "debug": "ExecutionMagics",
        "file": "Other",
        "html": "DisplayMagics",
        "javascript": "DisplayMagics",
        "js": "DisplayMagics",
        "latex": "DisplayMagics",
        "markdown": "DisplayMagics",
        "memit": "MemoryProfilerMagics",
        "mprun": "MemoryProfilerMagics",
        "perl": "Other",
        "prun": "ExecutionMagics",
        "pypy": "Other",
        "python": "Other",
        "python2": "Other",
        "python3": "Other",
        "ruby": "Other",
        "script": "ScriptMagics",
        "sh": "Other",
        "svg": "DisplayMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "writefile": "OSMagics"
       },
       "line": {
        "alias": "OSMagics",
        "alias_magic": "BasicMagics",
        "autoawait": "AsyncMagics",
        "autocall": "AutoMagics",
        "automagic": "AutoMagics",
        "autosave": "KernelMagics",
        "bookmark": "OSMagics",
        "cat": "Other",
        "cd": "OSMagics",
        "clear": "KernelMagics",
        "colors": "BasicMagics",
        "conda": "PackagingMagics",
        "config": "ConfigMagics",
        "connect_info": "KernelMagics",
        "cp": "Other",
        "debug": "ExecutionMagics",
        "dhist": "OSMagics",
        "dirs": "OSMagics",
        "doctest_mode": "BasicMagics",
        "ed": "Other",
        "edit": "KernelMagics",
        "env": "OSMagics",
        "gui": "BasicMagics",
        "hist": "Other",
        "history": "HistoryMagics",
        "killbgscripts": "ScriptMagics",
        "ldir": "Other",
        "less": "KernelMagics",
        "lf": "Other",
        "lk": "Other",
        "ll": "Other",
        "load": "CodeMagics",
        "load_ext": "ExtensionMagics",
        "loadpy": "CodeMagics",
        "logoff": "LoggingMagics",
        "logon": "LoggingMagics",
        "logstart": "LoggingMagics",
        "logstate": "LoggingMagics",
        "logstop": "LoggingMagics",
        "ls": "Other",
        "lsmagic": "BasicMagics",
        "lx": "Other",
        "macro": "ExecutionMagics",
        "magic": "BasicMagics",
        "man": "KernelMagics",
        "matplotlib": "PylabMagics",
        "memit": "MemoryProfilerMagics",
        "mkdir": "Other",
        "more": "KernelMagics",
        "mprun": "MemoryProfilerMagics",
        "mv": "Other",
        "notebook": "BasicMagics",
        "page": "BasicMagics",
        "pastebin": "CodeMagics",
        "pdb": "ExecutionMagics",
        "pdef": "NamespaceMagics",
        "pdoc": "NamespaceMagics",
        "pfile": "NamespaceMagics",
        "pinfo": "NamespaceMagics",
        "pinfo2": "NamespaceMagics",
        "pip": "PackagingMagics",
        "popd": "OSMagics",
        "pprint": "BasicMagics",
        "precision": "BasicMagics",
        "prun": "ExecutionMagics",
        "psearch": "NamespaceMagics",
        "psource": "NamespaceMagics",
        "pushd": "OSMagics",
        "pwd": "OSMagics",
        "pycat": "OSMagics",
        "pylab": "PylabMagics",
        "qtconsole": "KernelMagics",
        "quickref": "BasicMagics",
        "recall": "HistoryMagics",
        "rehashx": "OSMagics",
        "reload_ext": "ExtensionMagics",
        "rep": "Other",
        "rerun": "HistoryMagics",
        "reset": "NamespaceMagics",
        "reset_selective": "NamespaceMagics",
        "rm": "Other",
        "rmdir": "Other",
        "run": "ExecutionMagics",
        "save": "CodeMagics",
        "sc": "OSMagics",
        "set_env": "OSMagics",
        "store": "StoreMagics",
        "sx": "OSMagics",
        "system": "OSMagics",
        "tb": "ExecutionMagics",
        "time": "ExecutionMagics",
        "timeit": "ExecutionMagics",
        "unalias": "OSMagics",
        "unload_ext": "ExtensionMagics",
        "who": "NamespaceMagics",
        "who_ls": "NamespaceMagics",
        "whos": "NamespaceMagics",
        "xdel": "NamespaceMagics",
        "xmode": "BasicMagics"
       }
      },
      "text/plain": [
       "Available line magics:\n",
       "%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %conda  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %memit  %mkdir  %more  %mprun  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode\n",
       "\n",
       "Available cell magics:\n",
       "%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%memit  %%mprun  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile\n",
       "\n",
       "Automagic is ON, % prefix IS NOT needed for line magics."
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lsmagic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 功能二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2s(all_dict):\n",
    "    time2s=time-timedelta(seconds=2)\n",
    "    M=0\n",
    "    for t in list(all_dict[SID].keys()):\n",
    "        if t>=time2s:\n",
    "            for I in list(all_dict[SID][t].keys()):\n",
    "                M=all_dict[SID][t][I]\n",
    "        else:\n",
    "                del(all_dict[SID][t])\n",
    "    return all_dict,M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<timed exec>:8: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
      "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f31b25bd0c8f444ea280c980d216ce7b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 1.93 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "#11min 35s\n",
    "RRL_Orderbook = csv.DictReader(open('simple_data.csv'))\n",
    "\n",
    "import tqdm\n",
    "money_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(float)))#股票-时间-人-成交金额\n",
    "_last = defaultdict(lambda: None)\n",
    "\n",
    "for order in tqdm.tqdm_notebook(RRL_Orderbook):\n",
    "    SID=order[\"SecurityID\"]\n",
    "    time=timetoHHMMSSF(order[\"TradTime\"])\n",
    "    price=float(order[\"TradPrice\"])\n",
    "    volume=float(order[\"TradVolume\"])\n",
    "    flag=order[\"TradeBSFlag\"]\n",
    "    SellID=order[\"TradeSellNo\"]\n",
    "    BuyID=order[\"TradeBuyNo\"]\n",
    "    money=float(order[\"TradeMoney\"])\n",
    "\n",
    "    if flag==\"B\":\n",
    "        money_dict[SID][time][BuyID] += money\n",
    "        money_dict,M=test2s(money_dict)\n",
    "        if M>=50000000 and _last[SID] != (time, BuyID):\n",
    "            print (\"成交金额:{}, 股票代码:{}, 时间:{}, 交易人员代码:{}\".format(M,SID,str(time)[10:],BuyID))\n",
    "            _last[SID] = (time, BuyID)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 功能三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test60(all_dict,fluc_money,in_volume):\n",
    "    fluc_money=float(fluc_money)\n",
    "    M_B=0\n",
    "    M_S=0\n",
    "    a=0\n",
    "    for t in list(all_dict[SID].keys()):\n",
    "        for I in list(all_dict[SID][t].keys()):\n",
    "            flag=all_dict[SID][t][I][2]\n",
    "            if flag==\"B\":\n",
    "                v=all_dict[SID][t][I][0]\n",
    "                M_B+=all_dict[SID][t][I][1]*v\n",
    "            elif flag==\"S\":\n",
    "                v=all_dict[SID][t][I][0]\n",
    "                M_S+=all_dict[SID][t][I][1]*v\n",
    "            else:\n",
    "                pass\n",
    "    M=round(M_B-M_S,2)\n",
    "    if (M>=fluc_money):\n",
    "        a=1\n",
    "    else:\n",
    "        pass\n",
    "    return all_dict,a,M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<timed exec>:9: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0\n",
      "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dde86755f89849599acdfd60398d5839",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "资金净流入：50008623.12,股票代码：600166,时间： 09:33:26.150000,交易人员代码:733881\n",
      "Wall time: 2.46 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "dataline=csv.DictReader(open(\"simple_data.csv\"))\n",
    "volume_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(float)))#股票-时间-人-订单量\n",
    "all_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: None)))#股票-时间-人-（总订单量，价格,flag)\n",
    "all_SID = defaultdict(lambda: None)#股票出现过就不再显示\n",
    "\n",
    "fluc_money=50000000;in_volume=200000\n",
    "time1=timetoHHMMSSF(\"9:30:00.00\")\n",
    "time2=timetoHHMMSSF(\"10:30:00.00\")\n",
    "for order in tqdm.tqdm_notebook(dataline):\n",
    "    SID=order[\"SecurityID\"]\n",
    "    time=timetoHHMMSSF(order[\"TradTime\"])\n",
    "    volume=float(order[\"TradVolume\"])\n",
    "    price=float(order[\"TradPrice\"])\n",
    "    flag=order[\"TradeBSFlag\"]\n",
    "    SellID=order[\"TradeSellNo\"]\n",
    "    BuyID=order[\"TradeBuyNo\"]\n",
    "    money=float(order[\"TradeMoney\"])\n",
    "    #list_SID=['204001','204002','204003','204004','204005','204006','204007','204014','204028','204091','204182']\n",
    "    if time>=time1 and all_SID[SID] != SID :\n",
    "        ID=None\n",
    "        if flag==\"B\":\n",
    "            ID=BuyID\n",
    "        elif flag==\"S\":\n",
    "            ID=SellID\n",
    "        else:\n",
    "            continue\n",
    "        volume_dict[SID][time][ID]+=volume\n",
    "        all_dict[SID][time][ID]=(volume_dict[SID][time][ID],price,flag)\n",
    "        if  volume_dict[SID][time][ID] >= in_volume : \n",
    "            all_dict,a,M=test60(all_dict,fluc_money,in_volume)\n",
    "            if a==1:\n",
    "                print (\"资金净流入：{},股票代码：{},时间：{},交易人员代码:{}\".format(M,SID,str(time)[10:],ID))\n",
    "                all_SID[SID] = SID\n",
    "    elif time >=time2:\n",
    "        break\n",
    "    else:\n",
    "        continue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 功能四\n",
    "### 就是功能一将参数改一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
